package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.projectCenter.bean.PjInternalBean;
import com.koron.css2.projectCenter.dto.*;
import com.koron.css2.projectCenter.mapper.PjInternalMapper;
import com.koron.css2.projectCenter.service.PjInternalService;
import com.koron.css2.projectCenter.vo.PjInternalDetailVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 内部工程-保存、修改
 * 
 * @author 甄玉晴 2020年5月15日
 */
public class PjInternalSave implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			// 将入参转换为Bean
			PjInternalDetailVO bean  = JsonUtils.objectToPojo(req.getData(), PjInternalDetailVO.class);
			
			PjInternalBean pjInternalBean = getPjBeanFromVO(bean);
			String checkResult = null;
			// 判断主键是否为空，空的为添加操作，否则为修改操作
			if (StringUtils.isBlank(pjInternalBean.getId())) {
				// 检查节点字段
				checkResult = checkNode(pjInternalBean, "register");
				if (!"OK".equals(checkResult)) {
					return MessageBean.create(Constant.NOT_NULL, checkResult, null);
				}
				// 保存登记信息
				String internalId = savePjInternal(factory, userInfo, pjInternalBean, PjInternalService.REGISTER_NODE);
				// 返回ID
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", internalId);
				msg.setData(map);
				BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
				if(!StringUtils.isBlank(pjInternalBean.getTempId()))
					mapper.updateAccessoryReceiptId(internalId, pjInternalBean.getTempId());
			} else {
				// 检查节点编号
				String nodeCode = pjInternalBean.getProcessState();
				if (StringUtils.isBlank(nodeCode)) {
					nodeCode = PjInternalService.REGISTER_NODE;
				}
				if (!StringUtils.equalsAny(nodeCode, PjInternalService.REGISTER_NODE, PjInternalService.BUDGET_NODE,
						PjInternalService.CHECK_NODE, PjInternalService.INSTALL_NODE, PjInternalService.SETMT_NODE,
						PjInternalService.SURVEY_DESIGN_NODE)) {
					return MessageBean.create(Constant.NOT_NULL, "流程状态不存在。", null);
				}
				// 检查节点字段
				checkResult = checkNode(pjInternalBean, nodeCode);
				if (!"OK".equals(checkResult)) {
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, checkResult, null);
				}
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", pjInternalBean.getId());
				msg.setData(map);
				// 保存内部工程
				savePjInternal(factory, userInfo, pjInternalBean, nodeCode);
			}
			// 输出执行结果
			return msg;
		} catch (Exception e) {
			factory.close(false);
			logger.error("用水咨询保存失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}

	/**
	 * 保存内部工程
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public String savePjInternal(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean,
			String nodeCode) {
		String internalId = null;
		// 登记节点
		if (StringUtils.equalsAny(nodeCode, PjInternalService.REGISTER_NODE)) {
			internalId = saveRegister(factory, userInfo, pjInternalBean);
		}
		// 勘察设计节点
		if (StringUtils.equalsAny(nodeCode, PjInternalService.SURVEY_DESIGN_NODE)) {
			saveSurvey(factory, userInfo, pjInternalBean);
		}

		// 预算节点
		if (StringUtils.equals(nodeCode, PjInternalService.BUDGET_NODE)) {
			saveBudget(factory, userInfo, pjInternalBean);
		}
		// 安装施工节点
		if (StringUtils.equals(nodeCode, PjInternalService.INSTALL_NODE)) {
			saveInstall(factory, userInfo, pjInternalBean);
		}
		// 工程验收节点
		if (StringUtils.equals(nodeCode, PjInternalService.CHECK_NODE)) {
			saveCheck(factory, userInfo, pjInternalBean);
		}
		// 工程结算节点
		if (StringUtils.equals(nodeCode, PjInternalService.SETMT_NODE)) {
			saveSetmt(factory, userInfo, pjInternalBean);
		}
		return internalId;
	}

	/**
	 * 保存登记信息
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjInternalBean
	 * @param ctmId
	 * @param accountId
	 */
	public String saveRegister(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		String internalId = "";
		if (StringUtils.isBlank(pjInternalBean.getId())) {
			// 新增
			pjInternalBean.setCreateInfo(userInfo);
			pjInternalBean.setBillNo(Tools.getBillNo(factory, "PJ_INTERNAL"));// 统一编号生成方法
			pjInternalBean.setBillDate(CommonUtils.getCurrentDate());
			factory.getMapper(PjInternalMapper.class).addRegister(pjInternalBean);
			internalId = pjInternalBean.getId();
		} else {
			// 修改
			pjInternalBean.setUpdateName(userInfo.getUserInfo().getName());
			pjInternalBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(PjInternalMapper.class).updateRegister(pjInternalBean);
		}
		return internalId;
	}

	/**
	 * 保存勘察设计
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public void saveSurvey(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		pjInternalBean.setUpdateInfo(userInfo);
		;
		factory.getMapper(PjInternalMapper.class).updateSurveyDesign(pjInternalBean);
	}

	/**
	 * 保存预算
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public void saveBudget(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		pjInternalBean.setUpdateName(userInfo.getUserInfo().getName());
		pjInternalBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
		factory.getMapper(PjInternalMapper.class).updateBudget(pjInternalBean);
	}

	/**
	 * 保存安装施工
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public void saveInstall(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		pjInternalBean.setUpdateInfo(userInfo);
		factory.getMapper(PjInternalMapper.class).updateInstall(pjInternalBean);
	}
	/**
	 * 保存工程验收
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public void saveCheck(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		pjInternalBean.setUpdateInfo(userInfo);
		factory.getMapper(PjInternalMapper.class).updateCheck(pjInternalBean);
	}
	/**
	 * 保存工程结算
	 * 
	 * @param factory
	 * @param userInfo
	 * @param pjinternalBean
	 */
	public void saveSetmt(SessionFactory factory, UserInfoBean userInfo, PjInternalBean pjInternalBean) {
		pjInternalBean.setUpdateInfo(userInfo);
		factory.getMapper(PjInternalMapper.class).updateSetmt(pjInternalBean);
	}

	/**
	 * 检查登记节点
	 * 
	 * @param pjinternalBean
	 * @param ctmInfoBean
	 * @param accountInfoBean
	 * @return
	 */
	public String checkNode(PjInternalBean pjInternalBean, String nodeCode) {
		String checkResult = "";
		// 登记节点
		if (StringUtils.equalsAny(nodeCode, PjInternalService.REGISTER_NODE)) {
			checkResult = checkRegister(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 主键字段
		if (StringUtils.equalsAny(nodeCode, PjInternalService.SURVEY_DESIGN_NODE)) {
			checkResult = checkPrimaryKey(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 勘察设计节点
		if (StringUtils.equalsAny(nodeCode, PjInternalService.SURVEY_DESIGN_NODE)) {
			checkResult = checkSurveyDesign(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 预算节点
		if (StringUtils.equals(nodeCode, PjInternalService.BUDGET_NODE)) {
			checkResult = checkBudget(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 安装施工节点
		if (StringUtils.equals(nodeCode, PjInternalService.INSTALL_NODE)) {
			checkResult = checkInstall(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 工程验收节点
		if (StringUtils.equals(nodeCode, PjInternalService.CHECK_NODE)) {
			checkResult = checkCheck(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		// 工程验收节点
		if (StringUtils.equals(nodeCode, PjInternalService.SETMT_NODE)) {
			checkResult = checkSetmt(pjInternalBean);
			if (!"OK".equals(checkResult)) {
				return checkResult;
			}
		}
		return "OK";
	}

	/**
	 * 主键字段检查
	 * 
	 * @param pjinternalBean
	 * @param ctmInfoBean
	 * @param accountInfoBean
	 * @return
	 */
	public String checkPrimaryKey(PjInternalBean pjinternalBean) {
		if (StringUtils.isBlank(pjinternalBean.getId())) {
			return "主键不能为空。";
		}
		return "OK";
	}

	/**
	 * 登记字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkRegister(PjInternalBean pjInternalBean) {
		if(StringUtils.isBlank(pjInternalBean.getPjType())) {
			return "工程类型不能为空。";
		} else if (StringUtils.isBlank(pjInternalBean.getUrgentFlag() + "")) {
			return "紧急程度不能为空。";
		}
		return "OK";
	}

	/**
	 * 勘察设计字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkSurveyDesign(PjInternalBean pjInternalBean) {
		if (StringUtils.isBlank(pjInternalBean.getSurveyName())) {
			return "勘察人员不能为空。";
		} else if (StringUtils.isBlank(pjInternalBean.getSurveyDate())) {
			return "勘察日期不能为空。";
		} else if (StringUtils.isBlank(pjInternalBean.getDesignName())) {
			return "设计人员不能为空。";
		} else if (StringUtils.isBlank(pjInternalBean.getDesignDate())) {
			return "设计日期不能为空。";
		}
		return "OK";
	}

	/**
	 * 预算字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkBudget(PjInternalBean pjinternalBean) {
		if (pjinternalBean.getBudgetMoney() == null) {
			return "预算合计不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getBudgetName())) {
			return "编制人员不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getBudgetDate())) {
			return "编制日期不能为空。";
		}
		return "OK";
	}

	/**
	 * 安装施工字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkInstall(PjInternalBean pjinternalBean) {
		if (StringUtils.isEmpty(pjinternalBean.getPdDate())) {
			return "派单日期不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getJobName())) {
			return "施工班组不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getJobDate())) {
			return "施工开始时间不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getOverDate())) {
			return "施工结束时间不能为空。";
		}
		return "OK";
	}

	/**
	 * 工程验收字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkCheck(PjInternalBean pjinternalBean) {
		if (StringUtils.isEmpty(pjinternalBean.getCheckName())) {
			return "验收人员不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getCheckDate())) {
			return "验收日期不能为空。";
		}
		return "OK";
	}
	/**
	 * 工程结算字段检查
	 * 
	 * @param pjinternalBean
	 * @return
	 */
	public String checkSetmt(PjInternalBean pjinternalBean) {
		if (pjinternalBean.getSetmtTotal()==0) {
			return "结算合计不能为空。";
		} else if (StringUtils.isBlank(pjinternalBean.getSetmtCostSkname())) {
			return "结算编制人员不能为空。";
		}else if (StringUtils.isBlank(pjinternalBean.getSetmtCostDate())) {
			return "结算编制日期不能为空。";
		}
		return "OK";
	}
	/**
	 * 将前端传的值转成单一对象
	 * @param vo
	 * @return
	 */
	public PjInternalBean getPjBeanFromVO(PjInternalDetailVO vo) {
		PjInternalBean bean = new PjInternalBean();
		PjInternalRegisterDto register = vo.getRegister();
		PjInstallSurveyDesignDto surveyDesign = vo.getSurveyDesign();
		PjInternalBudgetDto budget = vo.getBudget();
		PjInternalInstallDto install = vo.getInstall();
		PjInternalCheckDto check = vo.getCheck();
		PjInternalSetmtDto setmt = vo.getSetmt();
		
		bean.setId(vo.getId());
		bean.setBillNo(vo.getBillNo());
		bean.setBillNo(vo.getBillDate());
		if(register!=null) {
			bean.setPjType(register.getPjType());
			bean.setApplName(register.getApplName());
			bean.setUrgentFlag(register.getUrgentFlag());
			bean.setPjAddr(register.getPjAddr());
			bean.setLinkMan(register.getLinkMan());
			bean.setLinkTel(register.getLinkTel());
			bean.setApplDesc(register.getApplDesc());
			bean.setAcceptDesc(register.getAcceptDesc());
			bean.setInpjName(register.getInpjName());
		}
		if(surveyDesign!=null) {
			bean.setPlFlag(surveyDesign.getPlFlag());
			bean.setExceedHigh(surveyDesign.getExceedHigh());
			bean.setSurveyName(surveyDesign.getSurveyName());
			bean.setSurveyDate(surveyDesign.getSurveyDate());
			bean.setSurveyResult(surveyDesign.getSurveyResult());
			bean.setDesignName(surveyDesign.getDesignName());
			bean.setDesignDate(surveyDesign.getDesignDate());
			bean.setDesignDesc(surveyDesign.getDesignDesc());
		}
		
		if(budget!=null) {
			bean.setBudgetMoney(budget.getBudgetMoney());
			bean.setBudgetName(budget.getBudgetName());
			bean.setBudgetDate(budget.getBudgetDate());
			bean.setBudgetPrepareNote(budget.getBudgetPrepareNote());
		}
		
		if(install!=null) {
			bean.setPdDate(String.valueOf(install.getPdDate()));
			bean.setJobName(install.getJobName());
			bean.setJobDate(install.getJobDate());
			bean.setOverDate(install.getOverDate());
			bean.setConstructionNote(install.getConstructionNote());
		}
		
		if(check!=null){
			bean.setCheckName(check.getCheckName());
			bean.setCheckDate(check.getCheckDate());
			bean.setCheckDesc(check.getCheckDesc());
		}
		
		if(setmt!=null){
			bean.setSetmtTotal(setmt.getSetmtTotal());
			bean.setSetmtCostSkname(setmt.getSetmtCostSkname());
			bean.setSetmtCostDate(setmt.getSetmtCostDate());
			bean.setSetmtNote(setmt.getSetmtNote());
		}
		
		
		bean.setProcessInstanceId(vo.getProcessInstanceId());
		bean.setProcessState(vo.getProcessState());
		bean.setTempId(vo.getTempId());
		
		return bean;
	}
}
